﻿package com.tudou.ad.track
{
    import com.adobe.net.*;
    import com.tudou.ad.consts.*;
    import com.tudou.ad.data.*;
    import com.tudou.ad.interfaces.*;
    import com.tudou.config.*;
    import com.tudou.js.*;
    import com.tudou.model.*;
    import com.tudou.newPlayer.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class AdTracker extends Object
    {
        protected var playerStatus:IPlayerStatus;
        protected var startTime:uint;
        protected var clickTimer:Timer;
        protected var baseInfo:IBaseInfo;
        private static var instance:AdTracker;

        public function AdTracker()
        {
            return;
        }// end function

        protected function newSwfLoad(param1:String) : void
        {
            var tdl:TdLoader;
            var url:* = param1;
            if (!url)
            {
                return;
            }
            try
            {
                trace("newSwfLoad : " + url);
                tdl = new TdLoader();
                tdl.load(url);
            }
            catch (e:Error)
            {
                trace("newSwfLoad error " + e.toString());
            }
            return;
        }// end function

        public function sendCptClick(param1:IAdThrowInfo, param2:IAdItemInfo) : void
        {
            var _loc_3:URLRequest = null;
            var _loc_4:URLVariables = null;
            if (param1 == null || param2 == null)
            {
                return;
            }
            _loc_3 = new URLRequest(Config.AD_CPT_CLICK_URL);
            _loc_4 = new URLVariables();
            _loc_4.juid = baseInfo.juid;
            _loc_4.userId = baseInfo.userId;
            _loc_4.adPositionId = param2.id;
            _loc_4.throwId = param2.aid;
            _loc_4.ownerId = param2.oid;
            _loc_4.link = param2.link;
            _loc_4.isBar = 0;
            _loc_3.data = _loc_4;
            navigateToURL(_loc_3);
            if (param2.nielson)
            {
                NielsenTracker.send(NielsenTracker.CLICK, param1, param2);
            }
            return;
        }// end function

        protected function ioErrorHlr(event:IOErrorEvent) : void
        {
            var _loc_2:* = event.target as DynamicURLLoader;
            var _loc_3:* = _loc_2.url;
            ErrorReportTracker.send(baseInfo.iid, ErrorReportTracker.ADPLAY_IOERROR, "adplayIOError");
            ErrorTracker.send(baseInfo.sourceId, ErrorTracker.ADPLAY_IOERROR, 0, _loc_3);
            return;
        }// end function

        public function init(param1:IPlayerStatus, param2:IBaseInfo) : void
        {
            baseInfo = param2;
            playerStatus = param1;
            startTime = getTimer();
            clickTimer = new Timer(1000, 0);
            clickTimer.start();
            return;
        }// end function

        private function replaceVar(param1:String, param2:String, param3:String) : String
        {
            var _loc_4:* = new RegExp("\\{\\$" + param2 + "\\}", "ig");
            return param1.replace(_loc_4, param3);
        }// end function

        protected function send(param1:String, param2:Array) : void
        {
            var _loc_3:URLRequest = null;
            var _loc_4:URLVariables = null;
            if (param2 != null)
            {
                _loc_3 = new URLRequest();
                _loc_4 = new URLVariables();
                _loc_4.adPlay = TudouAdPlay.getValue();
                _loc_4.s = param2.join("~_~");
                _loc_4.rinfo = baseInfo.rinfo;
                _loc_3.url = param1;
                _loc_3.data = _loc_4;
                _loc_3.method = URLRequestMethod.POST;
                sendTrackingRequest(_loc_3);
            }
            return;
        }// end function

        public function sendEffectiveShow(param1:IAdThrowInfo, param2:IAdItemInfo) : void
        {
            return;
        }// end function

        public function sendClick(param1:IAdThrowInfo, param2:IAdItemInfo, param3:String = "") : void
        {
            var _loc_4:Array = null;
            if (param1 == null || param2 == null)
            {
                return;
            }
            _loc_4 = ["21", param2.id, param2.type, param2.aid, baseInfo.iid, param1.areaCode, param1.channel, baseInfo.juid, param2.price, param2.oid, baseInfo.adSourceId, getTimer() - startTime, param1.virtualChannel, param3, baseInfo.userId, (param2.orderPos + 1), baseInfo.tpa];
            if (param2.nielson)
            {
                NielsenTracker.send(NielsenTracker.CLICK, param1, param2);
            }
            addOuterMonitorVars(param2.scu, param1, param2);
            send(Config.AD_CLICK_URL, _loc_4);
            return;
        }// end function

        public function sendComplete(param1:IAdThrowInfo, param2:IAdItemInfo) : void
        {
            var _loc_3:Array = null;
            if (param1 == null || param2 == null)
            {
                return;
            }
            _loc_3 = ["23", param2.id, param2.type, param2.aid, baseInfo.iid, param1.areaCode, param1.channel, baseInfo.juid, param2.price, param2.oid, baseInfo.adSourceId, param1.virtualChannel, baseInfo.userId, param2.cl, (param2.orderPos + 1), baseInfo.tpa];
            addOuterMonitorVars(param2.ufo, param1, param2);
            send(Config.AD_COMPLETE_URL, _loc_3);
            return;
        }// end function

        public function adExpect(param1:int) : void
        {
            var _loc_2:* = AdData.getInst();
            var _loc_3:* = _loc_2.adThrowInfo;
            var _loc_4:Array = [];
            [].push(27);
            _loc_4.push(baseInfo.iid);
            _loc_4.push(playerStatus.totalTime);
            _loc_4.push(_loc_3.channel);
            _loc_4.push(baseInfo.adSourceId);
            _loc_4.push(_loc_3.areaCode);
            _loc_4.push(param1);
            _loc_4.push(_loc_2.adIds.join(","));
            _loc_4.push(baseInfo.userId);
            _loc_4.push(baseInfo.viewState);
            _loc_4.push(baseInfo.tpa);
            _loc_4.push(baseInfo.juid);
            var _loc_5:* = new URLRequest(Config.AD_NUMBER_EXPECT_URL);
            var _loc_6:* = new URLVariables();
            new URLVariables().s = _loc_4.join("~_~");
            _loc_6.rinfo = baseInfo.rinfo;
            _loc_5.data = _loc_6;
            _loc_5.method = URLRequestMethod.GET;
            sendToURL(_loc_5);
            return;
        }// end function

        public function sendSkip(param1:IAdThrowInfo, param2:IAdItemInfo) : void
        {
            var _loc_3:Array = null;
            if (param1 == null || param2 == null)
            {
                return;
            }
            _loc_3 = ["22", param2.aid, param2.type, param2.id, baseInfo.adSourceId, param1.areaCode, param1.channel, param1.virtualChannel, baseInfo.iid, baseInfo.juid, baseInfo.userId, getTimer() - startTime, (param2.orderPos + 1), baseInfo.tpa];
            send(Config.AD_CLOSE_URL, _loc_3);
            return;
        }// end function

        public function sendPlay(param1:IAdThrowInfo, param2:IAdItemInfo) : void
        {
            var _loc_3:Array = null;
            if (param1 == null || param2 == null)
            {
                return;
            }
            _loc_3 = ["20", param2.id, param2.type, param2.aid, baseInfo.iid, param1.areaCode, param1.channel, baseInfo.juid, param2.price, param2.oid, baseInfo.adSourceId, param1.virtualChannel, baseInfo.userId, param2.cl, (param2.orderPos + 1), baseInfo.tpa];
            if (param2.nielson)
            {
                NielsenTracker.send(NielsenTracker.PLAY, param1, param2);
            }
            addOuterMonitorVars(param2.su, param1, param2);
            send(Config.AD_PLAY_URL, _loc_3);
            addJsMonitor(param2.jsUrl);
            return;
        }// end function

        private function addOuterMonitorVars(param1:String, param2:IAdThrowInfo, param3:IAdItemInfo) : void
        {
            var _loc_4:* = param1;
            _loc_4 = replaceVar(_loc_4, AdParam.RAND, RandString.noCache);
            _loc_4 = replaceVar(_loc_4, AdParam.CHANNEL_ID, param2.channel.toString());
            _loc_4 = replaceVar(_loc_4, AdParam.AREA_CODE, param2.areaCode.toString());
            _loc_4 = replaceVar(_loc_4, AdParam.IID, baseInfo.iid.toString());
            _loc_4 = replaceVar(_loc_4, AdParam.TID, param3.aid.toString());
            _loc_4 = replaceVar(_loc_4, AdParam.AD_PLAY, encodeURIComponent(TudouAdPlay.getValue()));
            addOuterMonitor(_loc_4, param3.st);
            return;
        }// end function

        protected function securityErrorHlr(event:SecurityErrorEvent) : void
        {
            var _loc_2:* = event.target as DynamicURLLoader;
            var _loc_3:* = _loc_2.url;
            ErrorReportTracker.send(baseInfo.iid, ErrorReportTracker.ADPLAY_SECURITY_ERROR, "adplaySecurityErr");
            ErrorTracker.send(baseInfo.sourceId, ErrorTracker.ADPLAY_SECURITY_ERROR, 0, _loc_3);
            return;
        }// end function

        protected function addOuterMonitor(param1:String, param2:int = 0) : void
        {
            var urls:Array;
            var i:int;
            var monitorUrl:* = param1;
            var st:* = param2;
            if (monitorUrl)
            {
                urls = monitorUrl.split(AdParam.SPLIT);
                i;
                while (i < urls.length)
                {
                    
                    if (st == 1)
                    {
                        newSwfLoad(urls[i]);
                    }
                    else
                    {
                        try
                        {
                            sendToURL(new URLRequest(urls[i]));
                        }
                        catch (e:Error)
                        {
                        }
                    }
                    i = (i + 1);
                }
            }
            return;
        }// end function

        protected function sendTrackingRequest(param1:URLRequest) : void
        {
            var _loc_2:* = new DynamicURLLoader();
            _loc_2.url = param1.url + "?" + param1.data;
            _loc_2.addEventListener(Event.COMPLETE, trackHandler);
            _loc_2.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHlr);
            _loc_2.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHlr);
            _loc_2.load(param1);
            return;
        }// end function

        protected function addJsMonitor(param1:String) : void
        {
            loadJavascript(param1);
            return;
        }// end function

        protected function trackHandler(event:Event) : void
        {
            var _loc_2:* = event.target as URLLoader;
            var _loc_3:* = _loc_2.data as String;
            if (_loc_3 != null && _loc_3 != "")
            {
                TudouAdPlay.setValue(_loc_3);
            }
            return;
        }// end function

        public static function getInstance() : AdTracker
        {
            if (instance == null)
            {
                instance = new AdTracker;
            }
            return instance;
        }// end function

    }
}
